<?php
require_once("head.php");
if (!is_user($user))die ("<center><font class=\"btitle\">"._ARAM."</font></center>");
$identifiant=identifiant();
######################################
function afficher_la_liste_des_sondages() {
######################################
global $lang,$user;

         $sql="SELECT q.id,q.texte_$lang as question,count(v.id) as nombre  FROM sondage_questions as q,sondage_votes as v where q.id=v.id_question  and type>0 And date_fin < ".date("Y-m-d")." group by q.id" ;
         #echo $sql."<br>";
         $result = mysql_query($sql);
         #echo mysql_errno().": ".mysql_error()."<BR>";
                                   echo "<table width=\"468\"  cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td>\n";
                                   while($row=mysql_fetch_array($result,MYSQL_ASSOC)){
                                   #print_r($row);
                                   echo "<font class=\"bcontent\">".$row["question"]."<font class=\"brtitle\">  [".$row["nombre"]."] </font> <a href=\"./sondage.php?op=afficher_resultat&id=".$row["id"]."\"><font class=\"blcontent\">"._VOIR_LES_REPONSES."</font></a>  <a href=\"./sondage.php?op=afficher_sondage&id=".$row["id"]."\"><font class=\"blcontent\">"._VOTER."</font></a>";
                                   if(is_admin($user))echo" <a href=\"./sondage.php?op=effacer_votes&id=".$row["id"]."\"><font class=\"brtitle\">"._EFFACER."</font></a>";
                                   echo"<br><br>";
                                   }
                                   echo"</td></tr></table></div>";

return;
}
######################################
function afficher_sondage($sondageid) {
######################################
global $dbi,$lang,$identifiant,$QUERY_STRING;

if(ereg("sondageid",$QUERY_STRING)){
$items=split("[=&]",$QUERY_STRING);
#print_r($items);
$sondageid= $items[1];
}

if( $id_choix_voter=verifier_si_voter($sondageid,$identifiant)){
$alert="<font class=\"brtitle\"> "._DEJA_VOTE."<br>"._MODIFIER_CHOIX."</font><br><br> ";
$op="modifier_le_vote";
$submit=_MODIFIER;
}else{

$op="enregistrer_le_vote";
$submit=_VOTER;
}

         $sql="SELECT q.id as id_question,q.texte_$lang as question,c.id as id_choix,c.texte_".$lang." as choix ,q.type FROM sondage_questions as q, sondage_choix as c where c.id_question=q.id AND q.id=$sondageid ORDER BY  c.id DESC " ;
         #echo $sql."<br>";
         $result = mysql_query($sql, $dbi);
                   #echo mysql_errno().": ".mysql_error()."<BR>";
               while($row=mysql_fetch_array($result,1)){
               #print_r($row);
                  $sondage["id"]=$row["id_question"];
                  $sondage["type"]=$row["type"];
                  $sondage["question"]=$row["question"];
                  $sondage["choix"][$n]["id"]=$row["id_choix"];
                  $sondage["choix"][$n++]["texte"]=$row["choix"];
               }
              #print_r($sondage);
              echo"
              $alert
              <table width=\"468\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >
              <tr>
              <td>
              <form name=\"form1\" method=\"post\" action=\"./sondage.php\">
              <input name=\"op\" type=\"hidden\" value=\"$op\">
              <input name=\"id_question\" type=\"hidden\" value=\"".$sondage["id"]."\">
              <input name=\"points\" type=\"hidden\" value=\"1\">
              <font class=\"bcontent\">".$sondage["question"]."</font><br>";
                          foreach($sondage["choix"] as $choix ){
                          echo "<font class=\"content\">".$choix["texte"]."</font>";
                                  if($sondage["type"]==5){
                                  note_etoiles($choix["id"]);
                                  }else{
                                  if($choix["id"]==$id_choix_voter)$checked="checked";else $checked="";
                                  echo" <input type=\"radio\" name=\"id_choix\" value=\"id_choix".$choix["id"]."\"$checked>
                                  <input name=\"Note".$choix["id"]."\" type=\"hidden\" value=\"1\"> <br>";
                                  }
                          }
              echo" <br>
                    <input type=\"submit\" name=\"Submit\" value=\"".$submit."\">
                    </form>
                    </td></tr></table></center> ";
}
##########################################
function verifier_si_voter($id_question,$identifiant) {
##########################################
global $dbi;
#print_r($HTTP_POST_VARS);
         $sql="SELECT id_choix FROM sondage_votes WHERE id_question='".$id_question."' AND identifiant='$identifiant'";
         #echo $sql."<br>\n";
         $resultat=mysql_query($sql, $dbi);
         #echo mysql_errno().": ".mysql_error()."<BR>";
         $row=mysql_fetch_array($resultat,1);
         if($row["id_choix"])return $row["id_choix"];
}



##################################################
function enregistrer_le_vote(){
##################################################
global $dbi,$HTTP_POST_VARS,$identifiant ;
if(verifier_si_voter($HTTP_POST_VARS["id_question"],$identifiant) AND $HTTP_POST_VARS["op"]=="enregistrer_le_vote" ):afficher_sondage($HTTP_POST_VARS["id_question"]);die();endif;
#print_r($HTTP_POST_VARS);

                          if($HTTP_POST_VARS["id_choix"]){
                             foreach($HTTP_POST_VARS as $items){
                                     if(ereg("id_choix",$items)){
                                     $id_choix=str_replace("id_choix","",$items);
                                     $votes[$n]["id_choix"]=$id_choix;
                                     $votes[$n++]["points"]=$HTTP_POST_VARS["Note".$id_choix] ;
                                     }
                              }
                 #print_r($votes);
                                if($HTTP_POST_VARS["op"]=="modifier_le_vote"):$action="UPDATE";$where="where id_question=".$HTTP_POST_VARS["id_question"]." AND identifiant='$identifiant'";
                                else:$action="INSERT";$where="";
                                endif;
                                foreach($votes as $vote ){

                                $sql="$action sondage_votes SET id_question=".$HTTP_POST_VARS["id_question"].",id_choix=".$vote["id_choix"].",points=".$vote["points"].", identifiant='$identifiant' $where";
                                #echo $sql."<br>\n";
                                mysql_query($sql, $dbi);
                                #echo mysql_errno().": ".mysql_error()."<BR>";
                                }

                          }

                   afficher_le_resultat_du_sondage($HTTP_POST_VARS["id_question"]);

}
######################################
function afficher_le_resultat_du_sondage_old($sondage_id) {
######################################
global $dbi,$lang;

         $sql="
         SELECT   c.id as id_choix,c.texte_".$lang." as choix, q.id as id_question,q.texte_$lang as question ,sum(v.points) as points,count(*) as nombre
         FROM sondage_questions as q, sondage_choix as c,sondage_votes as v
         where c.id_question=q.id AND v.id_question=q.id AND v.id_choix=c.id AND c.id_question=$sondage_id
         group by c.id " ;
         #echo $sql."<br>";
         $result = mysql_query($sql, $dbi);
         #echo mysql_errno().": ".mysql_error()."<BR>";
               while($row=mysql_fetch_array($result,1)){
               #print_r($row);
                  $sondage["id"]=$row["id_question"];
                  $sondage["nombre"]=$row["nombre"];
                  $sondage["question"]=$row["question"];
                  $sondage["choix"][$n]["id"]=$row["id_choix"];
                  $sondage["choix"][$n]["texte"]=$row["choix"];
                  $sondage["choix"][$n++]["points"]=$row["points"];
                  $sondage["total_points"]+=$row["points"];
               }
              #print_r($sondage);
              echo"<center>
              <table  border=\"0\" width=\"400\" cellspacing=\"0\" cellpadding=\"0\" class=\"content\">
              <tr><td colspan=\"2\" class=\"btitle\">".$sondage["question"]." <br><br></td></tr>";
              #echo count($sondage["choix"]) ;
              if(count($sondage["choix"])>1){
                            foreach($sondage["choix"] as $choix ){
                            echo "<tr><td width=\"150\">".$choix["texte"]."</td>
                            <td><img src=\"./images/couleurs/red.gif\" height=\"3\" width=\"".round((100/$sondage["total_points"])*$choix["points"],2)."\"> ".round((100/$sondage["total_points"])*$choix["points"],2)." % </td></tr>";
                            }

              }else{
               echo "<tr>
               <td bgcolor=\"gray\"  colspan=\"2\"><img src=\"./images/couleurs/red.gif\" height=\"20\" width=\"".(round(( $sondage["total_points"]/$sondage["nombre"]),2)*20)."%\"></td></tr>";
              }
               echo"<tr><td colspan=\"2\" class=\"bcontent\" >";
               if($sondage["type"]==5)echo (round(( $sondage["total_points"]/$sondage["nombre"]),2))."/5<br>"
                                      .(round(( $sondage["total_points"]/$sondage["nombre"]),2)*20)." %<br>";
               echo _VOTES_TOTAL." : ".$sondage["nombre"]."</td></tr>
              </table></center> ";



}######################################
function afficher_le_resultat_du_sondage($sondageid) {
######################################
global $dbi,$lang;

         $sql="
         SELECT   c.id as id_choix,c.texte_".$lang." as choix, q.id as id_question,q.texte_$lang as question
         FROM sondage_questions as q, sondage_choix as c
         where c.id_question=q.id AND c.id_question=$sondageid ORDER BY  c.id DESC";
         #echo $sql."<br>";
         $result = mysql_query($sql, $dbi);
         #echo mysql_errno().": ".mysql_error()."<BR>";
               while($row=mysql_fetch_array($result,1)){
               #print_r($row);
                  $sondage["id"]=$row["id_question"];
                  $sondage["question"]=$row["question"];
                  $sondage["choix"][$row["id_choix"]]["id"]=$row["id_choix"];
                  $sondage["choix"][$row["id_choix"]]["texte"]=$row["choix"];
                  $sondage["choix"][$row["id_choix"]]["points"]=0;
          $sql="
         SELECT  v.points as points,identifiant
         FROM sondage_votes as v
         where  v.id_question=".$row["id_question"]." AND v.id_choix=".$row["id_choix"];
         #echo $sql."<br>";
         $result2 = mysql_query($sql, $dbi);
         #echo mysql_errno().": ".mysql_error()."<BR>";
                  while($row2=mysql_fetch_array($result2,1)){
                  #print_r($row2);

                  $sondage["choix"][$row["id_choix"]]["points"]+=$row2["points"];
                  $sondage["choix"][$row["id_choix"]]["identifiants"][]=$row2["identifiant"];
                  $sondage["total_points"]+=$row2["points"];
                  }
               }
              #print_r($sondage);
              echo"<center>
              <table  border=\"0\" width=\"400\" cellspacing=\"0\" cellpadding=\"0\">
              <tr><td  class=\"btitle\">".$sondage["question"]." <br><br></td></tr>";
              #echo count($sondage["choix"]) ;
                            foreach($sondage["choix"] as $choix ){
                            echo "<tr><td width=\"150\" class=\"boxtitle\">".$choix["texte"]."(".$choix["points"].") </td>";
                            echo"<tr align=\"left\"><td height=\"110\" >";
                            if(is_array($choix["identifiants"])){
                             $where="AND j.identifiant IN ('".implode("','",$choix["identifiants"])."')";
                             afficher_joueurs(joueurs($where));
                            }
                            echo"</td></tr>";
                            echo"</tr>";
                            }

               echo"<tr><td colspan=\"2\" class=\"bcontent\" >";
               echo _VOTES_TOTAL." : ".$sondage["total_points"]."</td></tr>
              </table></center> ";



}
##########################################
function effacer_votes($id_question) {
##########################################
global $user,$dbi;
if(!is_admin($user))die("<center><font class=\"btitle\">"._ARAM."</font></center>");
 $sql="DELETE  FROM sondage_votes WHERE id_question='".$id_question."'";
         echo $sql."<br>\n";
         mysql_query($sql, $dbi);
         echo mysql_errno().": ".mysql_error()."<BR>";
return;
}
######################################

##################################################
switch($op) {
##################################################
case"afficher_sondage":
afficher_sondage($id);
break;

case"enregistrer_le_vote":
enregistrer_le_vote ();
break;

case"modifier_le_vote":
enregistrer_le_vote ();
break;

case"afficher_resultat":
afficher_le_resultat_du_sondage($id);
break;

case"effacer_votes":
effacer_votes($id);
break;

default:
afficher_la_liste_des_sondages();
if($sondage_email)afficher_sondage($sondageid);
break;
}
#if(!$printable)echo"<a href=\"".$HTTP_SERVER_VARS["REQUEST_URI"]."?&printable=1\"><img src=\"images/print.gif\" border=\"0\" alt=\""._PRINTER."\" title=\""._PRINTER."\" width=\"16\" height=\"11\"></a>\n";

######################################

?>